package com.igeek.health.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.health.constant.MessageConstant;
import com.igeek.health.entity.Checkgroup;
import com.igeek.health.entity.Checkitem;
import com.igeek.health.exception.HealthException;
import com.igeek.health.result.Result;
import com.igeek.health.service.ICheckgroupService;
import com.igeek.health.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author fengqq
 * @Description TODO
 * @Date 2024/8/27  15:57
 */
@RestController
@RequestMapping("/admin/checkgroup")
@Api(tags = "检查组的控制层")
public class CheckgroupController {

    @Autowired
    private ICheckgroupService checkgroupService;

    @ApiOperation("根据条件分页查询检查组")
    @GetMapping("/findPage")
    public Result findPage(@ApiParam("分页查询条件") PageQueryVO vo){
        Page<Checkgroup> page =  checkgroupService.findPage(vo);
        if (ObjectUtil.isNotEmpty(page)){
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,page);
        }
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    @ApiOperation("添加检查组信息")
    @PostMapping("/add")
    public Result add(
            @ApiParam("检查组信息") @RequestBody Checkgroup checkgroup,
            @ApiParam("勾选的检查项id列表") @RequestParam("checkitemIds")Integer[] checkitemIds){
        boolean flag = checkgroupService.add(checkgroup,checkitemIds);
        if(flag){
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
    }

    @ApiOperation("通过检查组id查询检查组信息")
    @GetMapping("/{id}")
    public Result findOne(@PathVariable("id") Integer id){
        Checkgroup checkgroup = checkgroupService.getById(id);
        if(ObjectUtil.isNotEmpty(checkgroup)){
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroup);
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    @ApiOperation("根据检查组编号，查询其包含的检查项信息")
    @GetMapping("/findCheckitemsByCheckgroupId/{id}")
    public Result  findCheckitemsByCheckgroupId(@PathVariable("id") Integer id){
        List<Integer> checkitemIds =  checkgroupService.findCheckitemsByCheckgroupId(id);
        if(CollectionUtil.isNotEmpty(checkitemIds)){
           return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
        }
        return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    @ApiOperation("根据检查组id编辑检查组信息")
    @PutMapping("/update")
    public Result update(
            @ApiParam("勾选的检查项列表") @RequestParam("checkitemIds") Integer[] checkitemIds,
            @ApiParam("检查组的信息")@RequestBody Checkgroup checkgroup){
        boolean flag = checkgroupService.update(checkitemIds,checkgroup);
        if(flag){
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }
        return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
    }


    @ApiOperation("根据检查组id删除信息")
    @DeleteMapping("/{id}")
    public Result delete(@ApiParam("检查组的id")@PathVariable("id") Integer id){
        boolean flag = checkgroupService.delete(id);
        if (flag){
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }
        return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
    }

    @ApiOperation("查询所有检查组信息")
    @GetMapping("/findAll")
    public Result findAll(){
       List<Checkgroup> checkgroupList =  checkgroupService.findAll();
       if(CollectionUtil.isNotEmpty(checkgroupList)){
           return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroupList);
       }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }


}
